package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;

    /**
     * 购物车添加
     * @param shoppingCart
     * @param session
     * @return
     */
    @PostMapping("/add")
    private R<ShoppingCart> addWith(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        Long user = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, user);
        Long dishId = shoppingCart.getDishId();
        shoppingCart.setUserId(user);
        if (dishId!=null){
            lqw.eq(ShoppingCart::getDishId,dishId);
            int count = shoppingCartService.count(lqw);
            if (count==0){
                shoppingCart.setNumber(1);

                shoppingCartService.save(shoppingCart);
                return R.success(shoppingCart);
            }
            ShoppingCart cart = shoppingCartService.getOne(lqw);
            Integer number = cart.getNumber();
            cart.setNumber(number+1);
            shoppingCartService.updateById(cart);
            return R.success(cart);
        }
        Long setmealId = shoppingCart.getSetmealId();
        lqw.eq(ShoppingCart::getSetmealId,setmealId);
        int count = shoppingCartService.count(lqw);
        if (count==0){
            shoppingCart.setNumber(1);

            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
        ShoppingCart cart = shoppingCartService.getOne(lqw);
        Integer number = cart.getNumber();
        cart.setNumber(number+1);
        shoppingCartService.updateById(cart);
        return R.success(cart);
    }

    /**
     * 购物车数量减一
     * @param shoppingCart
     * @param httpSession
     * @return
     */
    @PostMapping("/sub")
    private R<ShoppingCart> sub(@RequestBody  ShoppingCart shoppingCart,HttpSession httpSession){
        // 获取当前登录用户ID
        Long user = (Long) httpSession.getAttribute("user");
        // 条件构造器
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        if (dishId!=null&&dishId!=0){
            // 获取当前购物车数量
            lqw.eq(ShoppingCart::getDishId,dishId);

        }else{
            // 获取当前购物车数量
            lqw.eq(ShoppingCart::getSetmealId,setmealId);
        }
        lqw.eq(ShoppingCart::getUserId,user);
        ShoppingCart one = shoppingCartService.getOne(lqw);
        Integer number = one.getNumber();
        one.setNumber(number-1);
         shoppingCartService.saveOrUpdate(one);
        return R.success(one);
    }
}

